﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlcUI_test.MyControls
{
    public partial class FourRadioButton : UserControl
    {
        [Description("对应下位机属性名称")]
        public string ValueName { get; set; }

        [Description("第一个按钮名称")]
        public string Text1
        {
            get { return radioButton1.Text; }
            set { radioButton1.Text = value; }
        }

        [Description("第二个按钮名称")]
        public string Text2
        {
            get { return radioButton2.Text; }
            set { radioButton2.Text = value; }
        }

        [Description("第三个按钮名称")]
        public string Text3
        {
            get { return radioButton3.Text; }
            set { radioButton3.Text = value; }
        }

        [Description("第四个按钮名称")]
        public string Text4
        {
            get { return radioButton4.Text; }
            set { radioButton4.Text = value; }
        }

        /// <summary>
        /// 按钮对应的序号数
        /// </summary>
        public short Value { get; private set; }

        public event EventHandler<short> ButtonCheckedChanged;

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                Value = 0;
                ButtonCheckedChanged?.Invoke(this, Value);
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                Value = 1;
                ButtonCheckedChanged?.Invoke(this, Value);
            }
        }
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                Value = 2;
                ButtonCheckedChanged?.Invoke(this, Value);
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                Value = 3;
                ButtonCheckedChanged?.Invoke(this, Value);
            }
        }
        
        public void Inilialize()
        {
            radioButton1.Checked = true;// 默认选中第一个按钮
        }

        public FourRadioButton()
        {
            InitializeComponent();
            Value = 0;
            radioButton1.Checked = true;// 默认选中第一个按钮
            this.radioButton1.CheckedChanged += radioButton1_CheckedChanged;
            this.radioButton2.CheckedChanged += radioButton2_CheckedChanged;
            this.radioButton3.CheckedChanged += radioButton3_CheckedChanged;
            this.radioButton4.CheckedChanged += radioButton4_CheckedChanged;
        }
    }
}
